﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H



#include <QMainWindow>
#include <QProgressBar>
#include <QTreeWidget>
#include <QValueAxis>
#include <QDateTime>
#include <SendMessage/confirmdevice.h>
#include <QChart>

#include <QScatterSeries>
#include <QtCharts/QLineSeries>
#include <QChartView>
#include <QDateTimeAxis>
#include <QLabel>
#include <QProgressDialog>
#include <QTableWidget>
#include <QVBoxLayout>
#include <SendMessage/comchange.h>

class Dialog;
class DeviceConfigTab;
class DeviceChartsTSC;
class DockWidgetCTD_IMCharts;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

QT_CHARTS_USE_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    QDockWidget* dock=nullptr;
    QTabWidget* m_tabWidget=nullptr;
    Dialog* dialog=nullptr;
    ConfirmDevice* confirmDevice =nullptr;
    QString LocalDateTime="";
    double m_dT[4];
    double m_dP[4];

    double m_TSC[4];


    int m_index_CombomBox;
    QString label_Start_Status;
    QByteArray m_arrRemain;
    QList<double> m_vecTemperature;
    QList<time_t> m_vecDateTime;
    QList<double> m_vecPressure;
    QVector<double> m_vecK;
    QVector<double> m_vecN;

    void init_Layout();

    QString getHexToStr(QString hexStr);
    void setChannel();
    QTimer* m_Heart_timer;

    void setSampleStatus_01(bool);
    void setSampleStatus_02(bool);
    void setSampleStatus_Flag_03(bool);
    void setSampleStatus_not_sample(bool);
    QTreeWidgetItem* itemTD=nullptr;
    QTreeWidgetItem* itemT = nullptr;
    QTreeWidgetItem* itemCTD_IM = nullptr;
    QTreeWidgetItem* instrument=nullptr;
    QDockWidget* configDock=nullptr;
    QDockWidget* Bottom_Dock_Chart = nullptr;
    int hexCharToInt(char cHex);
    void set_Calibration_Coefficient_Temperature_Table();
    void set_Calibration_Coefficient_Pressure_Table();
    QList<QByteArray> ByteSplit(QByteArray& arrByte, const QByteArray& arrSplit_1, const QByteArray& arrSplit_2);
    QByteArray m_arrLast;
    time_t m_iStartTime;
    uint sampleInterval=0;
    bool m_bIM;
    void set_Bar_WidgetCenter(QWidget *pWidget);

 //   void set_Bottom_Charts();
    void set_Bottom_Charts_back();


    bool m_bFirstSampling;
    double m_dMaxT;
    double m_dMinT;
    double m_dMaxP;
    double m_dMinP;


    int timerId;
    QScatterSeries* m_scatterSeries_T;
    QScatterSeries* m_scatterSeries_P;
    QChart* m_chart;
    QLineSeries *m_dseries_Temperature;
    QLineSeries *m_dseries_Pressure;
    QDateTimeAxis* m_Time_daxisX;

    QValueAxis* daxisY_Temperature;
    QValueAxis* daxisY_Pressure;
    QChartView* dchartView;

    QString m_currDatetime;
    QTableWidget* m_Bottom_TableWidget;
    QHBoxLayout* m_hLayout;
    QVBoxLayout* m_vLayout;
    QTableWidgetItem* m_New_Value_Tem;
    QTableWidgetItem* m_New_Value_Press;
    QLabel* m_Label_Sample_Save_Location;
    QLabel* m_Label_Sample_Count;
    QLabel* m_Sample_Time;
    QToolBar* m_Start_Tb ;
    QPushButton* m_start_rotate;
    QWidget* m_Right_Bottom;
    int m_Sanple_count=0;
    bool m_Sample_Start_Flag_Time=true;
    bool m_isFirst_Simple=true;

    bool m_Start_Simple = true;

    QString m_Serial="";
    int m_iBarVal = 0;
    int m_iBarVal_count = 0;
    int m_iBarVal_count_old = 0;
    uint m_iPageQuantity=1;

    //QProgressDialog* down_File_Progress = nullptr;
    //QProgressDialog read_Flash_Progress;
    
    void SaveDownFile();
    void OriginSaveDownFile();

    bool m_bGetFlash;
    uint m_globle = 0;
    QProgressDialog* m_pDialog;
    bool bOK = false;
    QByteArray arrLED;

    QVector<int> ids;  // 所有ids的集合
    QList<QByteArray> Tms;   // 所有 Tms的集合
    QList<QByteArray> CoefficientAs;
    QList<QByteArray> CoefficientBs;
    QList<QByteArray> CoefficientCs;
    QList<QByteArray> CoefficientDs;
    QList<QByteArray> NotCoefficient;
    double Tm[5];
    double TA[5];
    double TB[5];
    double TC[5];
    double TD[5];
    double m_Pc[4];
    bool QBox = false;
    //double Tm;                  // 固定参考温度
    int DeviceId;
    QList<double> DeviceConfig;
    void matrix_vector_mult(const double matmat[][4], const double* vec, double* result, int rows, int cols);
    double vectors_dot_prod(const double* x, const double* y, int n);
    void read_config_file();
    DeviceConfigTab* deviceConfigTab = nullptr;
    DeviceChartsTSC* deviceChartsTSC;
    DockWidgetCTD* dockWidgetCTD_IM;
    DockWidgetCTD_IMCharts* dockWidgetCTD_IMCharts;
    bool configFlag = true;
protected:
    void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
    void closeEvent(QCloseEvent* event) override;

public slots:

    void HandleTypeData(Message_t);

    void HandleData(QString,QString);
    void HandleVoltageData(QString,QString);
    void HandlePlayData(QString,QString);
    void HandleDeviceTimeData(QString,QString);
    void syncStartDateTime(QString,QString);

    void setSampleConfiguration(QString,QString);
    void setTemperatureCoefficient(QString,QString);
    void setPressureCoefficientData(QString,QString);
    void setTemperatureABData(QByteArray,QByteArray);
    void setTemperatureCDData(QByteArray,QByteArray);

    void set_Pressure_CoefficientAB_Data(QByteArray,QByteArray);
    void set_Pressure_CoefficientCD_Data(QByteArray,QByteArray);
    void set_Calibration_Time(QByteArray,QByteArray);

    void set_Btn_Status(QByteArray);
    void set_Stop_After_Change_Status();

    void on_m_Button_SyncLocalTime_clicked();
    void set_Down_Load(QByteArray);

    void set_Once_Sample_Data_Chart_back(QByteArray,QByteArray);
    void setOnceSampleDataChartDebug(QByteArray data, QByteArray flag);
    void setStartSample(QByteArray data, QByteArray flag);
    void setProgressValue();

    void reciveTDSCComName(QString& portName);
    void reciveTSCComName(QString& portName);
    void setStopTimer();

    void getItem(QTreeWidgetItem*, int);
signals:
    void sendTemperatureCD(QByteArray);
    void sendTemperatureCoefficient(QByteArray,QByteArray);
    void sendPressureCoefficientAB(QByteArray);
    void send_Pressure_CoefficientCD(QByteArray);
    void send_Calibration_Time(QByteArray);
    void send_Get_Calibration_Coefficient();

    void send_Start_Sample(QByteArray);
    void send_Stop_Sample();

    void send_Read_Sample_Status();
    void send_Get_Sample_Config();

    void send_Read_Flash();

    void send_Once_Sample();

private slots:
    void on_m_radio_LocalDateTime_clicked(bool checked);
    void on_m_radioButton_Minute_clicked(bool checked);
    void on_m_radioButton_Hour_clicked(bool checked);
    void on_m_radioButton_Second_clicked(bool checked);
    void on_m_combomBox_Coefficient_currentIndexChanged(int index);
    void on_m_Ptn_SaveCoefficient_clicked();

    void on_m_Calibration_Coefficient_clicked();
    void on_m_pushButton_StartSample_clicked();
    void on_m_pbn_Save_Config_clicked();
    void on_m_Use_Last_Config_clicked();
    void on_m_pbn_DownLoad_clicked();


};

#define MainWindowRelease Singleton<MainWindow>::Release()

#define MainWindowInstance Singleton<MainWindow>::GetInstance()

#endif // MAINWINDOW_H
